package com.itcast.donkey.popwindow;

import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ExpandableListView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.itcast.donkey.R;
import com.itcast.donkey.adapters.PopMenuDetailJourneyAdapter;
import com.itcast.donkey.modles.DetailedJourneyModles;

/**
 * Created by lanouhn on 16/9/3.
 */
public class PopMenuDetailed extends PopupWindow {

    private TextView tvPopDetailedJourneyOutline;
    private TextView tvPopDetailedJourneyMap;
    private ExpandableListView listviewPopDetailedJourney;

    private View view;

    private DetailedJourneyModles modles;

    private PopMenuDetailJourneyAdapter adapter;

    public PopMenuDetailed(Context context, View.OnClickListener onClickListener, ExpandableListView.OnChildClickListener onItemClickListener, DetailedJourneyModles modles) {
        super(context);
        this.modles = modles;
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        view = inflater.inflate(R.layout.pop_detailed_journey, null);

        tvPopDetailedJourneyOutline = (TextView) view.findViewById(R.id.tv_pop_detailed_journey_outline);
        tvPopDetailedJourneyMap = (TextView) view.findViewById(R.id.tv_pop_detailed_journey_map);
        listviewPopDetailedJourney = (ExpandableListView) view.findViewById(R.id.listview_pop_detailed_journey);

        tvPopDetailedJourneyOutline.setOnClickListener(onClickListener);
        tvPopDetailedJourneyMap.setOnClickListener(onClickListener);
        listviewPopDetailedJourney.setOnChildClickListener(onItemClickListener);

        adapter = new PopMenuDetailJourneyAdapter(context, modles);
        listviewPopDetailedJourney.setAdapter(adapter);


        int groupCount = listviewPopDetailedJourney.getCount();
        for (int i = 0; i < groupCount; i++) {
            listviewPopDetailedJourney.expandGroup(i);
        }

        listviewPopDetailedJourney.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
            @Override
            public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {
                return true;
            }
        });


        //显示弹出框
        this.setContentView(view);
        this.setWidth(330);
        this.setHeight(800);
        //设置弹窗可点击
        this.setFocusable(true);
        //设置弹出窗体的动画效果
        this.setAnimationStyle(R.style.AppTheme);
        ColorDrawable cd = new ColorDrawable(0xb0000000);
        this.setBackgroundDrawable(cd);

        view.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                int height = view.findViewById(R.id.lay_pop).getTop();
                int y = (int) event.getY();
                if (event.getAction() == MotionEvent.ACTION_DOWN) {
                    if (y < height) {
                        dismiss();
                    }
                }
                return true;
            }
        });
    }
}
